package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Package;
import com.itheima.service.PackageService;
import com.itheima.util.QiNiuUtil;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class PackageController {

    @Reference
    private PackageService packageService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 查询检查组信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = packageService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }

    /**
     * 添加套餐
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Package packages,Integer[] checkgroupIds){
        packageService.add(packages,checkgroupIds);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
    }

    /**
     * 文件上传
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile)  {
        try {
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //获取后缀名
            int index = originalFilename.lastIndexOf(".");
            //获取文件后缀名
            String lastFileName = originalFilename.substring(index-1);
            //用时UUID为文件命名
            String newFileName = UUID.randomUUID()+lastFileName;

            QiNiuUtil.uploadViaByte(imgFile.getBytes(),newFileName);

            Map<String,String> map = new HashMap<String,String>();
            map.put("domain",QiNiuUtil.DOMAIN);
            map.put("newFileName",newFileName);

            Result result = new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,map);
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageInfo<Package> packageList = packageService.findPage(queryPageBean);
        PageResult<Package> pageResult = new PageResult<>(packageList.getTotal(),packageList.getList());
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

}